package com.daybook.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.daybook.pojo.Category;
import com.daybook.pojo.Member;
import com.daybook.service.MemberService;

@Controller
@RequestMapping(value = "/memberMsg", method = RequestMethod.POST)
public class MemberController {
	@Autowired
	private MemberService memberService;

	// 询获取所有会员折扣列表
	@RequestMapping(value = "/getAllMember", method = RequestMethod.POST)
	public void getAllMember(HttpServletRequest request, HttpServletResponse response)
			throws IOException, JSONException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		// 强制类型转换
		List<Member> stuList = memberService.selectAllMember();
		PrintWriter out = response.getWriter();
		JSONObject obj = new JSONObject();
		obj.put("res", 1);
		obj.put("list", stuList);
		out.print(obj);
	}

	// 修改会员名
	@RequestMapping(value = "/updateName", method = RequestMethod.POST)
	public void updateName(HttpServletRequest request, HttpServletResponse response) throws IOException, JSONException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		// 强制类型转换
		String mname=request.getParameter("mname");
		String id=request.getParameter("mid");
		int mid=Integer.valueOf(id);
		int result=memberService.updateMemberByUname(mid, mname);
		PrintWriter out = response.getWriter();
		JSONObject obj = new JSONObject();
		obj.put("res", result);
		
		out.print(obj);
		out.close();
	}
	// 修改会员1积分所需消费
		@RequestMapping(value = "/updateMoney", method = RequestMethod.POST)
		public void updateMoney(HttpServletRequest request, HttpServletResponse response) throws IOException, JSONException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			// 强制类型转换
			String mo=request.getParameter("money");
			double money=Double.valueOf(mo);
			int result=memberService.updateMember(money);
			PrintWriter out = response.getWriter();
			JSONObject obj = new JSONObject();
			obj.put("res", result);
			out.print(obj);
			out.close();
		}
}
